package com.hzvdvmc.autosale.setting.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.setting.bean.RoleEnum;
import com.hzvdvmc.autosale.setting.bean.Users;
import com.hzvdvmc.autosale.util.GsonUtils;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.Md5Util;
import com.hzvdvmc.autosale.util.SPUtils;
import com.hzvdvmc.autosale.util.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyPswActivity extends ActionbarActivity {
    @BindView(R.id.edt_old_psw)
    EditText edtOldPsw;
    @BindView(R.id.edt_new_psw)
    EditText edtNewPsw;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    private String account;
    private int rolePermission;


    @OnClick(R.id.btn_confirm)
    public void btnConfirm() {

        if (TextUtils.isEmpty(getOldPsw())) {
            ToastUtils.showShort(edtOldPsw.getHint().toString());
            return;
        }
        if (TextUtils.isEmpty(getNewPsw())) {
            ToastUtils.showShort(edtNewPsw.getHint().toString());
            return;
        }

        //超管，可以修改他人，但是不可修改自己
        String json = SPUtils.getInstance().getString(SpKey.USER_INFO);
        Users users = GsonUtils.GsonToBean(json, Users.class);
        if (users == null) {
            return;
        }

        HashMap<String, String> map = users.getMap();

        if (RoleEnum.Superadmin.getPermission() == rolePermission) {
            //超管修改他人密码
            boolean isContain = map.keySet().contains(getOldPsw());
            if (!isContain) {
                ToastUtils.showShort("账号不存在");
                return;
            }
            map.put(getOldPsw(), Md5Util.encode(getNewPsw()));
            LogUtil.p("超管修改了" + getOldPsw() + "的密码");

        } else {
            //非超管修改自己密码
            String psw = map.get(account);
            if (!psw.equals(Md5Util.encode(getOldPsw()))) {
                ToastUtils.showShort("旧密码输入错误");
                return;
            }
            map.put(account, Md5Util.encode(getNewPsw()));
            LogUtil.p(account + "修改了密码");
        }

        String userInfo = GsonUtils.GsonString(users);
        SPUtils.getInstance().put(SpKey.USER_INFO, userInfo);
        ToastUtils.showShort("密码修改成功");
    }

    public static void startMe(Context context, String account, int role) {
        Intent intent = new Intent(context, ModifyPswActivity.class);
        intent.putExtra("account", account);
        intent.putExtra("rolePermission", role);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_psw);
        ButterKnife.bind(this);
        parseIntent();
        setCustomeActiorBar("修改密码");
        if (RoleEnum.Superadmin.getPermission() == rolePermission) {
            edtOldPsw.setHint("请输入账号");

        }
    }

    public void clearInput() {
        edtOldPsw.getText().clear();
        edtNewPsw.getText().clear();
    }

    private void parseIntent() {
        account = getIntent().getStringExtra("account");
        rolePermission = getIntent().getIntExtra("rolePermission", 0);
    }

    public String getOldPsw() {
        return edtOldPsw.getText().toString();
    }

    public String getNewPsw() {
        return edtNewPsw.getText().toString();
    }

}
